#include "gameselect.h"
#include "gamehall.h"
#include "gameroom.h"

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(1200,900);
    this->setWindowTitle("关卡选择");
    this->setWindowIcon(QIcon(":res/ico.png"));

    //返回按钮
    QPushButton *backBtn = new QPushButton(this);
    backBtn->setIcon(QIcon(":res/back.png"));
    backBtn->move(this->width() * 0.02,this->height() * 0.02);
    backBtn->setFixedSize(50,50);
    connect(backBtn,&QPushButton::clicked,[=](){
        this->close();
        GameHall *gameHall = new GameHall;
        gameHall->show();

        QSound::play(":res/clicked.wav");
    });


    QFont font("华文行楷",30);
    // 简单模式
    QPushButton *simpleBtn = new QPushButton(this);
    QPushButton *normalBtn = new QPushButton(this);
    QPushButton *complexBtn = new QPushButton(this);

    simpleBtn->setFont(font);
    normalBtn->setFont(font);
    complexBtn->setFont(font);

    simpleBtn->setStyleSheet("QPushButton{border:0px;}");
    normalBtn->setStyleSheet("QPushButton{border:0px;}");
    complexBtn->setStyleSheet("QPushButton{border:0px;}");

    simpleBtn->move(this->width() * 0.1,this->height() * 0.8);
    normalBtn->move(this->width() * 0.3,this->height() * 0.8);
    complexBtn->move(this->width() * 0.5,this->height() * 0.8);

    simpleBtn->setText("简单模式");
    normalBtn->setText("正常模式");
    complexBtn->setText("复杂模式");

    Gameroom *gameRoom = new Gameroom;
    connect(simpleBtn,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->styleGameRoom(GameStyle::SIMPLE);
        gameRoom->snakeTimeout(300);
        gameRoom->show();

        QSound::play(":res/clicked.wav");
    });

    connect(normalBtn,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->styleGameRoom(GameStyle::NORMAL);//控制绘制食物形状
        gameRoom->snakeTimeout(200);//移动速度
        gameRoom->show();

        QSound::play(":res/clicked.wav");
    });

    connect(complexBtn,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->styleGameRoom(GameStyle::COMPLEX);
        gameRoom->snakeTimeout(100);
        gameRoom->show();

        QSound::play(":res/clicked.wav");
    });


    //历史战绩
    QPushButton *hisBtn = new QPushButton(this);
    hisBtn->setFont(font);
    hisBtn->move(this->width() * 0.7,this->height() * 0.8);
    hisBtn->setStyleSheet("QPushButton{border:0px;}");
    hisBtn->setText("历史战绩");

    connect(hisBtn,&QPushButton::clicked,[=](){
        QSound::play(":res/clicked.wav");

        QWidget *newWidget = new QWidget;
        newWidget->setFixedSize(this->width() * 0.5,this->height() * 0.5);
        newWidget->setWindowTitle("历史战绩");
        newWidget->setWindowIcon(QIcon(":res/ico.png"));

        QTextEdit *edit = new QTextEdit(newWidget);
        edit->setFixedSize(this->width() * 0.5, this->height() * 0.5);
        edit->setFont(font);

        QFile file("D:/SHIMIR/Documents/SnakesGame/history.txt");
        file.open(QIODevice::ReadOnly);

        QTextStream in(&file);
        int data = in.readLine().toInt();

        edit->append(QString("历史得分为："));
        edit->append(QString::number(data));

        newWidget->show();
    });

}

void GameSelect::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix(":res/game_select.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
